package com.akaovi.chatroom.scene;

import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import org.kordamp.ikonli.javafx.FontIcon;

import java.util.Optional;

/**
 * @author akaovi
 * @version 1.0
 * @description  好友栏
 * @date 2022/12/9 18:06
 * @since 2022/12/9 18:06
 */
public class FriendsList {
    public static VBox friendsList = new VBox();  // 好友栏
    static Font font = new Font(16);
    static Label friendsText = new Label("好友列表");

    static {
        friendsList.setSpacing(20);
        friendsList.setAlignment(Pos.BASELINE_CENTER);
        friendsList.setBackground(new Background(new BackgroundFill(new Color(0.98, 0.98, 0.98, 1),
                null, null)));
        friendsText.setId("-1");
        friendsText.setFont(new Font(18));
        friendsText.setGraphic(new FontIcon("anto-team"));
        friendsList.getChildren().add(friendsText);

        Platform.runLater(() -> MainScene.gridPane.add(friendsList, 1, 0, 2, 10));
        friendsList.setVisible(false);
    }

    /**
     * 添加一个好友节点
     * @param account 账号
     * @param username 用户名
     */
    public static void addFriendToFriendsList(int account, String username) {
        Platform.runLater(() -> {
            Label friend = new Label(username);
            friend.setId(String.valueOf(account));
            friend.setOnMouseClicked(e -> {
                Label beClicked = (Label) e.getSource();
                UserInfoBoard.username.setText(beClicked.getText());
                UserInfoBoard.account.setText(beClicked.getId());
                if (!UserInfoBoard.account.isVisible()) {
                    UserInfoBoard.showUserInfo();
                }
                // todo 调试
                System.out.println("好友列表点击事件");
            });
            friend.setGraphic(new FontIcon("anto-user"));
            friend.setFont(font);
            friendsList.getChildren().add(friend);
        });
    }

    /**
     * 展示
     */
    public static void showFriendsList() {
        if ("".equals(UserInfoBoard.account.getText())) {
            MainScene.showCommonIcon();
        } else {
            MainScene.coverCommonIcon();

        }
        friendsList.setVisible(true);
        if (ChatList.chatList.isVisible()) {
            ChatList.chatList.setVisible(false);
        }
    }

    /**
     * 不展示
     */
    public static void coverFriendsList() {
        if (friendsList.isVisible()) {
            friendsList.setVisible(false);
        }
    }

    /**
     * 重置
     */
    public static void restart() {
        FriendsList.friendsList.getChildren().clear();
        FriendsList.friendsList.getChildren().add(friendsText);
    }

    /**
     * 删除一个好友节点
     * @param account
     */
    public static void removeOne(String account) {
        Platform.runLater(() -> {
            Optional<Node> first = friendsList.getChildren().stream().filter(label -> label.getId().equals(account)).findFirst();
            first.ifPresent(node -> friendsList.getChildren().remove(node));
        });
    }
}

